package com.yy.main.app.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoleilu.hutool.log.StaticLog;
import com.yy.constant.AppConstant;
import com.yy.kit.collections.YYListKits;
import com.yy.kit.collections.YYMapKits;
import com.yy.kit.object.YYObjectKits;
import com.yy.kit.secure.YYSecureKits;
import com.yy.main.app.mapper.TPermissionMapper;
import com.yy.main.app.mapper.TUserMapper;
import com.yy.main.app.service.IUserService;
import com.yy.pojo.base.TPermission;
import com.yy.pojo.base.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 存储用户信息。
 * </p>
 *
 * @author 杨阳
 * @since 2017-08-22
 */
@Service("iUserService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private TUserMapper tUserMapper;
    @Autowired
    private TPermissionMapper tPermissionMapper;

    @Override
    public Map<String, Object> userLogin(String account, String password, HttpSession session) {
        Map<String, Object> map = YYMapKits.createHashMap();

        TUser user = tUserMapper.userLogin(account);
        if (YYObjectKits.isNull(user)) {
            map.put("success", Boolean.FALSE);
            map.put("msg", "用户帐号输入错误!");
        } else {
            String pwd = user.getPassword();
            if (pwd.equals(YYSecureKits.md5(password))) {
                // 登陆成功
                List<TPermission> perList = processingData(tPermissionMapper.getUserPermissions(user.getId()));
                session.setAttribute(AppConstant.SESSION_KEY, user);
                session.setAttribute(AppConstant.MENU_KEY, perList);
                map.put("success", Boolean.TRUE);
            } else {
                map.put("success", Boolean.FALSE);
                map.put("msg", "密码错误!");
            }
        }
        return map;
    }

    /**
     * <p>
     * 处理一下用户的权限数据，可以适合后台显示
     * </p>
     *
     * @param list 所有权限集合
     * @return {@link java.util.List( TPermission )}
     * @author 杨阳
     * @since 2017年8月31日16:32:44
     */
    private List<TPermission> processingData(List<TPermission> list) {
        StaticLog.info("============================================");
        List<TPermission> result = YYListKits.createList();

        if (YYListKits.isNotEmpty(list)) {
            for (TPermission permission : list) {
                // 子节点的集合
                List<TPermission> childList = YYListKits.createList();
                if (permission.getLevel() == 1) {// 一级菜单
                    String per_id = permission.getId();
                    for (TPermission child : list) {
                        if (child.getLevel() == 2) {
                            String parentid = child.getParentid();
                            if (per_id.equals(parentid)) {
                                childList.add(child);
                            }
                        }
                    }
                    permission.setChildList(childList);
                    result.add(permission);
                }
            }
            Collections.sort(result);

            StaticLog.info("resultList content is {} .", result);
            StaticLog.info("============================================");
            return result;
        } else {
            return YYListKits.createList();
        }
    }

    @Override
    public PageInfo userAssignList(Integer page, Integer limit, String status, String user_num, String name, String sex) {
        PageHelper.startPage(page, limit);
        List<TUser> userList = tUserMapper.userAssignList(status, user_num, name, sex);
        return new PageInfo(userList);
    }

}
